﻿//-----------------------------------------------------------------------
// <copyright file="AuctioneerRespositoryTests.cs" company="Transilvania University of Brasov">
//     Copyright (c) Nagy Robert. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace TestDataAccessLayer
{
    using System;
    using System.Collections.Generic;

    using DataAccessLayer.Auctioneer;
    using DomainModel;
    using Helpers;
    using NUnit.Framework;
    
    /// <summary>
    /// Tests the AuctioneerRepository class
    /// </summary>
    [TestFixture]
    public class AuctioneerRespositoryTests
    {
        /// <summary>
        /// The auctioneer repository
        /// </summary>
        private IAuctioneerRepository auctioneerRepository = new AuctioneerRepository();

        /// <summary>
        /// Tests the invalid entity should not be added.
        /// </summary>
        [Test]
        public void TestInvalidEntityShouldNotBeAdded()
        {
            Assert.Throws<ArgumentException>(() => this.auctioneerRepository.Add(new DTOAuctioneer()));
        }

        /// <summary>
        /// Tests the valid entity should be added.
        /// </summary>
        [Test, Rollback]
        public void TestValidEntityShouldBeAdded()
        {
            DTOAuctioneer auctioneer = new DTOAuctioneer
            {
                FirstName = "FirstName",
                LastName = "LastName",
                Password = "Password",
                Rating = 5m,
                Username = "Username"
            };

            this.auctioneerRepository.Add(auctioneer);
            DTOAuctioneer foundAuctioneer = this.auctioneerRepository.Find(auctioneer.Id);
        }

        /// <summary>
        /// Tests the invalid entity should not be edited.
        /// </summary>
        [Test]
        public void TestInvalidEntityShouldNotBeEdited()
        {
            Assert.Throws<ArgumentException>(() => this.auctioneerRepository.Edit(new DTOAuctioneer()));
        }

        /// <summary>
        /// Tests the valid entity should be edited.
        /// </summary>
        [Test, Rollback]
        public void TestValidEntityShouldBeEdited()
        {
            DTOAuctioneer auctioneer = AuctioneerTestHelper.CreateDatabaseEntry();

            auctioneer.FirstName = "NewFirstName";
            auctioneer.LastName = "NewLastName";
            auctioneer.Password = "NewPassword";
            auctioneer.Rating = 2.5m;
            auctioneer.Username = "NewUsername";

            this.auctioneerRepository.Edit(auctioneer);

            DTOAuctioneer foundAuctioneer = this.auctioneerRepository.Find(auctioneer.Id);

            Assert.NotNull(foundAuctioneer);
            Assert.AreEqual(auctioneer.FirstName, foundAuctioneer.FirstName);
            Assert.AreEqual(auctioneer.Id, foundAuctioneer.Id);
            Assert.AreEqual(auctioneer.LastName, foundAuctioneer.LastName);
            Assert.AreEqual(auctioneer.Password, foundAuctioneer.Password);
            Assert.AreEqual(auctioneer.Rating, foundAuctioneer.Rating);
            Assert.AreEqual(auctioneer.Username, foundAuctioneer.Username);
        }

        /// <summary>
        /// Tests the invalid identifier should not delete.
        /// </summary>
        /// <param name="id">The identifier.</param>
        [Test]
        public void TestInvalidIdShouldNotDelete([Values(-1, 0)] int id)
        {
            Assert.Throws<ArgumentException>(() => this.auctioneerRepository.Delete(id));
        }

        /// <summary>
        /// Tests the valid identifier should delete.
        /// </summary>
        [Test, Rollback]
        public void TestValidIdShouldDelete()
        {
            DTOAuctioneer auctioneer = AuctioneerTestHelper.CreateDatabaseEntry();
            this.auctioneerRepository.Delete(auctioneer.Id);

            DTOAuctioneer foundAuctioneer = this.auctioneerRepository.Find(auctioneer.Id);
            Assert.IsNull(foundAuctioneer);
        }

        /// <summary>
        /// Tests the get all.
        /// </summary>
        [Test, Rollback]
        public void TestGetAll()
        {
            DTOAuctioneer auctioneer = AuctioneerTestHelper.CreateDatabaseEntry();
            List<DTOAuctioneer> auctioneerList = this.auctioneerRepository.GetAll();
            Assert.IsTrue(auctioneerList.Exists(a => a.FirstName.Equals(auctioneer.FirstName) &&
                                        a.LastName.Equals(auctioneer.LastName) &&
                                        a.Password.Equals(auctioneer.Password) &&
                                        a.Rating == auctioneer.Rating &&
                                        a.Username.Equals(auctioneer.Username)));
        }
    }
}
